/**
 * @Author: zclin
 */
let app = getApp()
let formatTime = require('../../utils/util.js')
let NORMAL = 0, LOWER = 1, PULLDOWN = 2 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentList: [],
    commentListBackup: [],
    offset: 1,
    limit: 10,
    loading: false,
    loadingComplete: false,
    loaded: false,
    noConnect: false,
    labelList: ['全部', '好评', '中评', '差评'],
    activeIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.setData({
      id: e.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDataList(NORMAL)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 获取全部评论列表
   */
  getCommentList: function (flag) {
    let token = wx.getStorageSync('token')
    let that = this
    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }
    wx.request({
      url: app.apiUrl('/product/comment'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        product_id: that.data.id,
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {
        console.log(res.data)
        let loadingComplete = false
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          let commentList = res.data.data.comments
          let loadingComplete = false
          for (var i = 0; i < commentList.length; i++) {
            commentList[i].avatar_url = app.globalData.imgUrl + commentList[i].user.avater.savepath + commentList[i].user.avater.savename
            commentList[i].creation_time = formatTime.formatDate(new Date(formatTime.getDateDiff(commentList[i].created_at)))
            for (var j = 0; j < commentList[i].product_comment_file.length; j++) {
              commentList[i].product_comment_file[j].url = app.globalData.imgUrl + commentList[i].product_comment_file[j].file.savepath + commentList[i].product_comment_file[j].file.savename
            }
          }

          if (!(res.data.data.comments.length == that.data.limit)) {
            loadingComplete = true
          }

          that.setData({
            commentList: commentList,
            commentListBackup: commentList,
            loadingComplete: loadingComplete
          })

        }
      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })
        wx.showModal({
          title: '提示',
          content: '网络请求异常',
          showCancel: false
        })
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
   * 获取好评列表
   */
  getGoodCommentList: function (flag) {
    let token = wx.getStorageSync('token')
    let that = this
    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }
    wx.request({
      url: app.apiUrl('/product/goodproductcomment'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        product_id: that.data.id,
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          let commentList = res.data.data.comments
          let loadingComplete = false

          for (var i = 0; i < commentList.length; i++) {
            commentList[i].avatar_url = app.globalData.imgUrl + commentList[i].user.avater.savepath + commentList[i].user.avater.savename
            commentList[i].creation_time = formatTime.formatDate(new Date(formatTime.getDateDiff(commentList[i].created_at)))
            for (var j = 0; j < commentList[i].product_comment_file.length; j++) {
              commentList[i].product_comment_file[j].url = app.globalData.imgUrl + commentList[i].product_comment_file[j].file.savepath + commentList[i].product_comment_file[j].file.savename
            }
          }

          if (!(res.data.data.comments.length == that.data.limit)) {
            loadingComplete = true
          }

          that.setData({
            commentList: commentList,
            commentListBackup: commentList,
            loadingComplete: loadingComplete
          })

        }
      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })
        wx.showModal({
          title: '提示',
          content: '网络请求异常',
          showCancel: false
        })
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
   * 获取中评列表
   */
  getMediumCommentList: function (flag) {
    let token = wx.getStorageSync('token')
    let that = this
    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }
    wx.request({
      url: app.apiUrl('/product/mediumproductcomment'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        product_id: that.data.id,
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          let commentList = res.data.data.comments
          let loadingComplete = false
          for (var i = 0; i < commentList.length; i++) {
            commentList[i].avatar_url = app.globalData.imgUrl + commentList[i].user.avater.savepath + commentList[i].user.avater.savename
            commentList[i].creation_time = formatTime.formatDate(new Date(formatTime.getDateDiff(commentList[i].created_at)))
            for (var j = 0; j < commentList[i].product_comment_file.length; j++) {
              commentList[i].product_comment_file[j].url = app.globalData.imgUrl + commentList[i].product_comment_file[j].file.savepath + commentList[i].product_comment_file[j].file.savename
            }
          }

          if (!(res.data.data.comments.length == that.data.limit)) {
            loadingComplete = true
          }

          that.setData({
            commentList: commentList,
            commentListBackup: commentList,
            loadingComplete: loadingComplete
          })

        }
      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })
        wx.showModal({
          title: '提示',
          content: '网络请求异常',
          showCancel: false
        })
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
   * 获取差评列表
   */
  getBadCommentList: function (flag) {
    let token = wx.getStorageSync('token')
    let that = this
    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }
    wx.request({
      url: app.apiUrl('/product/badproductcomment'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        product_id: that.data.id,
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          let commentList = res.data.data.comments
          let loadingComplete = false
          for (var i = 0; i < commentList.length; i++) {
            commentList[i].avatar_url = app.globalData.imgUrl + commentList[i].user.avater.savepath + commentList[i].user.avater.savename
            commentList[i].creation_time = formatTime.formatDate(new Date(formatTime.getDateDiff(commentList[i].created_at)))
            for (var j = 0; j < commentList[i].product_comment_file.length; j++) {
              commentList[i].product_comment_file[j].url = app.globalData.imgUrl + commentList[i].product_comment_file[j].file.savepath + commentList[i].product_comment_file[j].file.savename
            }
          }

          if (!(res.data.data.comments.length == that.data.limit)) {
            loadingComplete = true
          }

          that.setData({
            commentList: flag == LOWER ? that.data.commentList.concat(commentList) : commentList,
            commentListBackup: flag == LOWER ? that.data.commentList.concat(commentList) : commentList,
            loadingComplete: loadingComplete
          })

        }
      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })
        wx.showModal({
          title: '提示',
          content: '网络请求异常',
          showCancel: false
        })
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.offset = 1
    this.setData({
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false
    })
    
    wx.showNavigationBarLoading()
    this.getDataList(PULLDOWN)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("toLower" + this.data.loadingComplete)
    this.setData({
      loading: this.data.loading,
      loadingComplete: this.data.loadingComplete
    })
    if (!this.data.loadingComplete) {
      this.data.offset += 1
      this.getDataList(LOWER)

    }
  },

  /**
   * 隐藏转发按钮
   */
  hideShareButton: function () {
    if (wx.hideShareMenu)
      wx.hideShareMenu()
  },


  /**
   * 点击标签
   */
  labelTap: function (e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      activeIndex: index
    })

    this.setData({
      offset: 1,
      limit: 10,
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false,
    })

    this.getDataList(NORMAL)
    
  },

  /**
   * 筛选评论类型
   */
  filter: function (type) {
    let commentListTemp = this.data.commentListBackup
    let result = []
    if (type == 1) {
      for (var i = 0; i < commentListTemp.length; i++) {
        if (commentListTemp[i].star > 4)
          result.push(commentListTemp[i])
      }
    } else if (type == 2) {
      for (var i = 0; i < commentListTemp.length; i++) {
        if (commentListTemp[i].star > 2 && commentListTemp[i].star <=4)
          result.push(commentListTemp[i])
      }
    } else if (type == 3) {
      for (var i = 0; i < commentListTemp.length; i++) {
        if (commentListTemp[i].star <= 2)
          result.push(commentListTemp[i])
      }
    } else {
      result = commentListTemp
    }

    this.setData({
      commentList: result
    })
  },

  /**
   * 获取数据列表
   */
  getDataList: function (flag) {
    if (this.data.activeIndex == 0)
      this.getCommentList(flag)
    else if (this.data.activeIndex == 1)
      this.getGoodCommentList(flag)
    else if (this.data.activeIndex == 2)
      this.getMediumCommentList(flag)
    else if (this.data.activeIndex == 3)
      this.getBadCommentList(flag)

  }
})